<div class="box-tools" style="top:10px;">
    <form class="form-inline" id="project—form" action="{:url('ProjectSide/ProjectDaiFu')}" method="GET" pjax-search="">
        <div class="form-group">
            <label for="project_id">项目名称</label>
            <select class="form-control" id="project_id" name="project_id" style="border-radius:3px;">
                <option value="0">全部</option>
                {volist name="projectList" id="vo"}
                <option value="{$vo.id}" {if condition=" $vo.id eq $project_id "}selected="selected"{/if} >{$vo.project_name}</option>
                {/volist}
            </select>
        </div>
        <div class="form-group">
            <label for="money">代付金额</label>
            <input type="text" name="money" id="money" class="form-control" placeholder="输入代付金额后默认筛选" style="border-radius:3px;"
                   value="{$money}" />
        </div>
        {if condition="empty($money) || $money eq 0"}
        <div class="form-group">
            <label for="series_number">申请期数</label>
            <input type="hidden" name="relevance_id" id="relevance_id" class="form-control"value="{:input('get.relevance_id')}" />
            <select class="form-control" id="series_number" name="series_number" style="border-radius:3px;">
                <option value="0" relevance=''>全部</option>
            </select>
        </div>
        <div class="form-group">
            <label for="status">状态</label>
            <select class="form-control" id="status" name="status" style="border-radius:3px;">
                <option value="-1">全部</option>
                <option value="0" {if condition="$status eq 0" }selected="selected" {/if} >未代付</option>
                <option value="1" {if condition="$status eq 1" }selected="selected" {/if} >已代付</option>
                <option value="2" {if condition="$status eq 2" }selected="selected" {/if} >代付失败</option>
            </select>
        </div>
        {/if}
        <div class="form-group">
            <button type="submit" class="btn btn-primary">搜索</button>
            <button type="button" id="dwonload" class="btn btn-primary ">下载</button>
            <button type="button" class="btn btn-primary" onclick="resetForm()">重置</button>
        </div>
    </form>
</div>
<script>
    $(function () {
        $('#project—form').submit(function () {
            if($('#money').val() !== ''){
                if($('#project_id').val() === '0'){
                    $.amaran({'message': '请选择项目!'});
                    return false
                }
            }
        })
        $('#dwonload').click(function () {
             var url = "{:url('ProjectSide/ProjectDaiFu')}";
             var project_id = $("select[name='project_id']").val();
             var status = $("select[name='status']").val();
             var money = $("input[name='money']").val();
             var series_number =  $('select[name="series_number"]').val();
             var relevance_id = $("input[name='relevance_id']").val();
             var afm_id = "{$afm_id}";
            var has_bind_list="{$has_bind_list}";
             location.href = url + "?project_id=" + project_id + "&status=" + status + "&money=" + money + "&series_number=" + series_number + "&down=1&relevance_id="+relevance_id+'&afm_id={$afm_id}&has_bind_list={$has_bind_list}';
        })
        if($("#project_id").children('option:selected').val() !== '0'){
            let projectId = $("#project_id").children('option:selected').val()
            getNum(projectId)
        }
        if($('#money').val() !== ''){
            resetSomeForm(true)
        } else {
            resetSomeForm(false)
        }
        $('#money').on('keyup',function () {
            if($(this).val() !== '') {
                resetSomeForm(true)
            } else {
                resetSomeForm(false)
            }
        })
        $('#project_id').on('change', function (e) {
            let projectId = $(this).children('option:selected').val()
            getNum(projectId)
        })
        $('#series_number').on('change', function (e) {
            $('#relevance_id').val($(this).children('option:selected').attr('relevance'))
        })
    })
    // 充值部分form
    function  resetSomeForm(isResetNormal){
        if(isResetNormal){
            $('#project_id').prev().addClass('hry-require')
            $('#series_number').attr('disabled', true)
            $('#status').attr('disabled', true)
            $('#series_number').val('0');
            $('#relevance_id').val('');
        } else{
            $('#project_id').prev().removeClass('hry-require')
            $('#series_number').attr('disabled', false)
            $('#status').attr('disabled', false)
        }
    }
    // 获取期数
    function getNum(projectId) {
        $.ajax({
            url: 'getSeriesNumber',
            data: {id: projectId},
            type: "GET",
            dataType: "json",
            success: function (result) {
                if (result.status == 1) {
                    let search = window.location.search.split('&')
                    let seriesNnumber=null
                    if(search.length ===5){
                        seriesNnumber = search[3].split('=')[1]
                    }
                    $('#series_number').empty()
                    $('#series_number').append("<option value='0' relevance=''>全部</option>")
                    if(result.datas.length>0){
                        result.datas.forEach(function (item, index) {
                            $('#series_number').append("<option value='"+item.series_number+"' relevance='"+item.relevance_id+"'>"+item.series_number_string+"</option>")
                        })
                    } else {
                        $('#relevance_id').val('')
                    }
                    $('#series_number option[value='+seriesNnumber+']').attr("selected","selected");
                } else {
                    $('#series_number').empty()
                    $('#series_number').append("<option value='0' relevance=''>全部</option>")
                    $('#relevance_id').val('')
                }
            },
            error: function (result, xhr, status, errorThrown) {
                $.amaran({'message': result.info})
            },
            complete: function (xhr, status) {
//                    alert('The request is complated');
            },
            beforesend: function () {
            },
            cache: false,//true
            async: true
        })
    }
    function resetForm() {
        $('select[name="status"]').val('-1')
        $('select[name="project_id"]').val('0')
        $('input[name="money"]').val('')
        $('select[name="series_number"]').val('')
        $('#project—form').submit()
    }
</script>
